package cn.jiangjiesheng.bootstrap.commons.context;

import cn.jiangjiesheng.bootstrap.commons.exception.BizzException;
import cn.jiangjiesheng.bootstrap.commons.properties.DevUserMockProperties;
import com.alibaba.ttl.TransmittableThreadLocal;
//import com.qycloud.service.user.domain.UserSession;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

/**
 * Created by dev@jiangjiesheng.cn on 2021/9/26  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
@Slf4j
public class UserInfoContext {

    private static final TransmittableThreadLocal<UserInfo> userInfoThreadLocal = new TransmittableThreadLocal<>();

    public static void setUserInfo(@NonNull UserInfo userInfo) {
        userInfoThreadLocal.set(userInfo);
    }
//
//    public static void setUserInfo(@NonNull UserSession userSession) {
//        userInfoThreadLocal.set(UserInfo.builder()
//                        .isMocked(false)
//                        .enterpriseId(userSession.getContainerId())
//                        .userId(userSession.getUserId())
//                        .enterpriseName(userSession.getContainerName())
//                        .realName(userSession.getRealName())
//                .build());
//    }

    public static void setUserInfo(@NonNull DevUserMockProperties devUserMockProperties){
        UserInfo userInfo = UserInfo.builder()
                .enterpriseId(devUserMockProperties.getEntId())
                .userId(devUserMockProperties.getUserId())
                .enterpriseName(devUserMockProperties.getEntId())
                .realName("mock名称")
                .isMocked(true)
                .build();
        setUserInfo(userInfo);
    }

    public static UserInfo getUserInfo() {
        return userInfoThreadLocal.get();
    }

    public static boolean isUserLogin() {
        return Objects.nonNull(getUserInfo()) && StringUtils.isNotBlank(getUserInfo().getUserId());
    }

    public static String getCurContainerId(){
        if(!isUserLogin()){
            throw new BizzException("未登录");
        }
        return getUserInfo().getContainerId();
    }

    public static String getCurUserId(){
        if(!isUserLogin()){
            throw new BizzException("未登录");
        }
        return getUserInfo().getUserId();
    }

    public static void reset() {
        setUserInfo(UserInfo.builder().build());
    }

    @Getter
    @Builder
    public static class UserInfo implements Serializable {

        private boolean isMocked = false;

        private String enterpriseId;

        private String userId;

        private String avatar;

        private Integer sex;

        private String realName;

        private String enterpriseName;

        /**
         * accessToken
         */
        private String accessToken;

        public String getContainerId() {
            return enterpriseId;
        }

        public String getRealName(){
            return this.realName;
        }

    }
}
